package image.noise.generators;

import image.processing.ImageProcessor;
import image.processing.ImageProcessor.CompressAlgorithm;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.List;

import random.generators.DistributionGenerator;

public abstract class AdditiveNoise {
	protected DistributionGenerator generator;

	@Deprecated
	public BufferedImage addNoise(BufferedImage img, CompressAlgorithm algorithm) {
		BufferedImage ans;
		if (img == null) {
			return null;
		} else {
			List<Double> randoms = generator.generateRandomNumbers(img
					.getHeight() * img.getWidth());
			if (img.getType() == BufferedImage.TYPE_BYTE_GRAY) {
				ans = new BufferedImage(img.getWidth(), img.getHeight(),
						BufferedImage.TYPE_BYTE_GRAY);
				WritableRaster raster = ans.getRaster();
				int intValues[][] = new int[img.getWidth()][img.getHeight()];
				int k = 0;
				for (int x = 0; x < img.getWidth(); x++) {
					for (int y = 0; y < img.getHeight(); y++) {
						int valor = img.getRaster()
								.getSample(x, y, 0) +(int) Math.floor(randoms.get(k));
						System.out.println(valor);
						intValues[x][y] = valor;
						k++;
					}
				}
				if (algorithm == CompressAlgorithm.DYNAMIC) {
					intValues = ImageProcessor
							.DynamicRangeCompressionChannel(intValues);
				} else if (algorithm == CompressAlgorithm.LINEAR) {
					intValues = ImageProcessor
							.LinearRangeCompressionChannel(intValues);
				}
				for (int x = 0; x < img.getWidth(); x++) {
					for (int y = 0; y < img.getHeight(); y++) {
						raster.setSample(x, y, 0, intValues[x][y]);
					}
				}
			} else {
				List<Double> randoms1 = generator.generateRandomNumbers(img
						.getHeight() * img.getWidth());
				List<Double> randoms2 = generator.generateRandomNumbers(img
						.getHeight() * img.getWidth());
				ans = new BufferedImage(img.getWidth(), img.getHeight(),
						BufferedImage.TYPE_3BYTE_BGR);
				WritableRaster raster = ans.getRaster();
				int intValues0[][] = new int[img.getWidth()][img.getHeight()];
				int intValues1[][] = new int[img.getWidth()][img.getHeight()];
				int intValues2[][] = new int[img.getWidth()][img.getHeight()];
				int i = 0, j = 0, k = 0;
				for (int x = 0; x < img.getWidth(); x++) {
					for (int y = 0; y < img.getHeight(); y++) {
						intValues0[x][y] = (int) Math.floor(img.getRaster()
								.getSample(x, y, 0) + randoms.get(i));
						intValues1[x][y] = (int) Math.floor(img.getRaster()
								.getSample(x, y, 1) + randoms1.get(j));
						intValues2[x][y] = (int) Math.floor(img.getRaster()
								.getSample(x, y, 2) + randoms2.get(k));
						i++;
						j++;
						k++;
					}
				}
				if (algorithm == CompressAlgorithm.DYNAMIC) {
					intValues0 = ImageProcessor
							.DynamicRangeCompressionChannel(intValues0);
					intValues1 = ImageProcessor
							.DynamicRangeCompressionChannel(intValues1);
					intValues2 = ImageProcessor
							.DynamicRangeCompressionChannel(intValues2);
				} else if (algorithm == CompressAlgorithm.LINEAR) {
					intValues0 = ImageProcessor
							.LinearRangeCompressionChannel(intValues0);
					intValues1 = ImageProcessor
							.LinearRangeCompressionChannel(intValues1);
					intValues2 = ImageProcessor
							.LinearRangeCompressionChannel(intValues2);
				}
				for (int x = 0; x < img.getWidth(); x++) {
					for (int y = 0; y < img.getHeight(); y++) {
						raster.setSample(x, y, 0, intValues0[x][y]);
						raster.setSample(x, y, 1, intValues1[x][y]);
						raster.setSample(x, y, 2, intValues2[x][y]);
					}
				}
			}
			return ans;
		}
	}
}
